//
//  CESettingsViewController.m
//  C8 Energy
//
//  Created by 颜魏 on 2024/10/1.
//

#import "CESettingsViewController.h"
#import "DAConfig.h"
#import "NSBundle+DAUtils.h"
#import "CETabbarController.h"
#import "CEDeviceListViewController.h"
#import "CELogListViewController.h"
#import "DeviceSettingViewController.h"
#import "UpgardeViewController.h"

@interface CESettingsViewController ()

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *chgTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *languageLabel;
@property (weak, nonatomic) IBOutlet UILabel *logStatusLabel;
@property (weak, nonatomic) IBOutlet UILabel *logSecondsLabel;
@property (weak, nonatomic) IBOutlet UILabel *secondsLabel;
@property (weak, nonatomic) IBOutlet UILabel *lookLogLabel;
@property (weak, nonatomic) IBOutlet UILabel *resetLabel;
@property (weak, nonatomic) IBOutlet UILabel *pwdLabel;
@property (weak, nonatomic) IBOutlet UILabel *paramsLabel;
@property (weak, nonatomic) IBOutlet UILabel *cdjrLabel;
@property (weak, nonatomic) IBOutlet UILabel *fdjrLabel;
@property (weak, nonatomic) IBOutlet UILabel *prehotLabel;
@property (weak, nonatomic) IBOutlet UILabel *restartLabel;
@property (weak, nonatomic) IBOutlet UILabel *tipsLabel;
@property (weak, nonatomic) IBOutlet UISwitch *logSw;
@property (weak, nonatomic) IBOutlet UISwitch *cfdSW;
@property (weak, nonatomic) IBOutlet UISwitch *cdjrSW;
@property (weak, nonatomic) IBOutlet UISwitch *fdjrSW;
@property (weak, nonatomic) IBOutlet UILabel *versionLabel;
@property (weak, nonatomic) IBOutlet UILabel *preNumLabel;
@property (weak, nonatomic) IBOutlet UISwitch *restartSW;

@property (nonatomic, strong) NSTimer *queryTimer; //数据定时器

@property (nonatomic, assign) BOOL  isCurrentPage;
@property (nonatomic, assign) BOOL  isWrite;
@property (nonatomic, assign) NSInteger queryCount;

// 0.没有设置  1.设置充电  2，设置放电  3.设置重启
@property (assign, nonatomic) NSInteger  setType;
@property (nonatomic, assign) BOOL isUpgrading;

@end

@implementation CESettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isWrite = NO;
    self.queryCount = 0;
    self.setType = 0;
    [self setLanguage];
    NSString *time = [[NSUserDefaults standardUserDefaults] objectForKey:LogInterval];
    self.secondsLabel.text = [NSString stringWithFormat:@"%@s", (time && time.length) ? time : @"5"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(upgradeUpdate:) name:kBTUpgrading object:nil];

    NSString *log = [[NSUserDefaults standardUserDefaults] objectForKey:LogIsOpen];
    self.logSw.on = [log isEqualToString:@"on"];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReciveData:) name:kBTDidReciveData object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSettingState:) name:@"SettingPageState" object:nil];

    [self queryData];
}

- (void)upgradeUpdate:(NSNotification *)nf {
    self.isUpgrading = [nf.object integerValue] == 1;
}

- (void)dealloc {
    [self.queryTimer invalidate];
    self.queryTimer = nil;
}

- (void)queryData {
    WeakSelf
    self.queryTimer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
//        return;
        if (self.isUpgrading) return;
        if (!weakSelf.isCurrentPage || weakSelf.isWrite) {
            return;
        }
        if (weakSelf.queryCount % 2 == 0) {
            [weakSelf readStateCHGDSG];
        }else {
            [weakSelf readSwitchStatus];
        }
        weakSelf.queryCount ++;
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.isCurrentPage = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.isCurrentPage = NO;
}

- (void)setLanguage {
    self.titleLabel.text = NSLocalizedString(@"Parameter setting", nil);

    self.chgTitleLabel.text = NSLocalizedString(@"charge switch", nil);
    self.languageLabel.text = NSLocalizedString(@"Language settings", nil);
    self.logStatusLabel.text = NSLocalizedString(@"Log switch", nil);
    self.logSecondsLabel.text = NSLocalizedString(@"Log interval (seconds)", nil);
    self.lookLogLabel.text = NSLocalizedString(@"View log", nil);
    self.resetLabel.text = NSLocalizedString(@"Device reset", nil);
    self.pwdLabel.text = NSLocalizedString(@"Password setting", nil);
    self.paramsLabel.text = NSLocalizedString(@"Parameter setting", nil);
    self.cdjrLabel.text = NSLocalizedString(@"Charging heating", nil);
    self.fdjrLabel.text = NSLocalizedString(@"Discharge heating", nil);
    self.prehotLabel.text = NSLocalizedString(@"Preheat", nil);
    self.restartLabel.text = NSLocalizedString(@"Force start", nil);
    self.tipsLabel.text = NSLocalizedString(@"Automatically closes after opening", nil);
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    self.versionLabel.text = [NSString stringWithFormat:@"%@ V%@", NSLocalizedString(@"Software Version", nil), appVersion];
}

- (void)reloadLanguage {
    CBPeripheral *peripheral = [BTManager sharedBTManager].peripheral;
    [ProgressHUD showActivityMessageInView:@"" timer:2];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:IS_ChANGD_LANGUAGE];
        [[NSUserDefaults standardUserDefaults] synchronize];
        UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:[[CEDeviceListViewController alloc] initWithNibName:@"CEDeviceListViewController" bundle:nil]];
        UIWindow *window = [UIApplication sharedApplication].delegate.window;
        window.rootViewController = nv;
        [window makeKeyAndVisible];
        CETabbarController *tabbar = [[CETabbarController alloc] init];
        [nv pushViewController:tabbar animated:YES];
        tabbar.selectedIndex = 1;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[BTManager sharedBTManager] connectPeripheral:peripheral];
        });
    });
}

#pragma mark - action
- (IBAction)actionBack:(id)sender {
    [[BTManager sharedBTManager] disconnectPeripheral:[BTManager sharedBTManager].peripheral];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)actionLanguage:(id)sender {
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"Language", nil) preferredStyle:UIAlertControllerStyleActionSheet];
    [sheet addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Chinese", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        if ([[DAConfig userLanguage] hasPrefix:@"en"]) {
            [DAConfig setUserLanguage:@"zh-Hans"];
            [self reloadLanguage];
//        }
    }]];
    [sheet addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"English", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        if ([[DAConfig userLanguage] hasPrefix:@"zh"]) {
            [DAConfig setUserLanguage:@"en"];
            [self reloadLanguage];
//        }
    }]];
    [sheet addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:sheet animated:YES completion:nil];
}

- (IBAction)actionLog:(UISwitch *)sender {
    [[NSUserDefaults standardUserDefaults] setObject:(sender.isOn ? @"on" : @"off") forKey:LogIsOpen];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (IBAction)actionSetLogSeconds:(id)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Log interval", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:nil]];
    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Confirm", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *timeIntervalTextfield = alertController.textFields.firstObject;
        NSString *timeInterval = timeIntervalTextfield.text;
        if (timeInterval.integerValue>0) {
            [[NSUserDefaults standardUserDefaults]setObject:timeInterval forKey:LogInterval];
            self.secondsLabel.text = timeInterval;
        }
    }]];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = NSLocalizedString(@"Please enter the log interval", nil);
//           textField.secureTextEntry = YES;
        textField.keyboardType = UIKeyboardTypeNumberPad;
    }];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (IBAction)actionLookLog:(id)sender {
    CELogListViewController *vc = [[CELogListViewController alloc] initWithNibName:@"CELogListViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)actionCHG:(UISwitch *)sender {
    if (sender.on) {
        Byte byte[] = {0x01,0x06,0x11,0x02,0x00, 0x03, 0x6d, 0x37};
        [[BTManager sharedBTManager] sendCommand:byte length:8];
    }else {
        Byte byte[] = {0x01,0x06,0x11,0x03,0x00, 0x03, 0x3c, 0xf7};
        [[BTManager sharedBTManager] sendCommand:byte length:8];
    }
}

- (IBAction)actionReset:(id)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"Confirm Reset", nil) preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:nil]];
    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Confirm", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self writeClearProtection];
    }]];
    [self presentViewController:alertController animated:true completion:nil];
}

- (IBAction)actionSetPwd:(id)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Reset the password", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:nil]];
    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Confirm", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *oldTF = alertController.textFields.firstObject;
        UITextField *newTF = alertController.textFields.lastObject;
        NSString *oldPwd = [[NSUserDefaults standardUserDefaults] objectForKey:SetPassword];
        if (!oldPwd || oldPwd.length == 0) {
            oldPwd = @"LT1111";
        }
        if (![oldTF.text isEqualToString:oldPwd]) {
            [ProgressHUD showErrorMessage:NSLocalizedString(@"Password error", nil)];
            return;
        }
        if (!newTF.text.length) {
            [ProgressHUD showErrorMessage:NSLocalizedString(@"Please enter the new password", nil)];
            return;
        }
        [[NSUserDefaults standardUserDefaults] setObject:newTF.text forKey:SetPassword];
        [ProgressHUD showErrorMessage:NSLocalizedString(@"Modify successful", nil)];
    }]];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = NSLocalizedString(@"Please enter the old password", nil);
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = NSLocalizedString(@"Please enter the new password", nil);
    }];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (IBAction)actionSetParams:(id)sender {
//    DeviceSettingViewController *vc = [[DeviceSettingViewController alloc] initWithNibName:@"DeviceSettingViewController" bundle:nil];
//    [self.navigationController pushViewController:vc animated:YES];
//    return;
//
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Please enter the password", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:nil]];
    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Confirm", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *pwdTF = alertController.textFields.firstObject;
        NSString *oldPwd = [[NSUserDefaults standardUserDefaults] objectForKey:SetPassword];
        if (!oldPwd || oldPwd.length == 0) {
            oldPwd = @"LT1111";
        }
        if ([oldPwd isEqualToString:pwdTF.text]) {
            DeviceSettingViewController *vc = [[DeviceSettingViewController alloc] initWithNibName:@"DeviceSettingViewController" bundle:nil];
            [self.navigationController pushViewController:vc animated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangePageShowState" object:@"0"];
        }else {
            [ProgressHUD showErrorMessage:NSLocalizedString(@"Password error", nil)];
        }
    }]];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = NSLocalizedString(@"Please enter the password", nil);
    }];
    [self presentViewController:alertController animated:YES completion:nil];
}
- (IBAction)actionCDJR:(UISwitch *)sender {
    self.setType = 1;
    self.isWrite = YES;
    [ProgressHUD showActivityMessageInView:@"" timer:8];
    Byte write_array[8];
    write_array[0] = 0x01;
    write_array[1] = 0x06;
    write_array[2] = 0x11;
    write_array[3] = sender.on ? 0x02 : 0x03;
    write_array[4] = 0x00;
    write_array[5] = 0x0c;
    write_array[6] = 0x00;
    write_array[7] = 0x00;
    sleep(1);
    [[BTManager sharedBTManager] sendCommandWithCrc:write_array length:8];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.isWrite = NO;
        if (self.setType == 1) {
            self.cdjrSW.on = !self.cdjrSW.on;
            [ProgressHUD hideHUD];
            [ProgressHUD showSuccessMessage:NSLocalizedString(@"Set failed", nil)];
        }
    });
}
- (IBAction)actionFDJR:(UISwitch *)sender {
    self.setType = 2;
    self.isWrite = YES;
    [ProgressHUD showActivityMessageInView:@"" timer:8];
    Byte write_array[8];
    write_array[0] = 0x01;
    write_array[1] = 0x06;
    write_array[2] = 0x11;
    write_array[3] = sender.on ? 0x02 : 0x03;
    write_array[4] = 0x00;
    write_array[5] = 0x0d;
    write_array[6] = 0x00;
    write_array[7] = 0x00;
    sleep(1);
    [[BTManager sharedBTManager] sendCommandWithCrc:write_array length:8];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.isWrite = NO;
        if (self.setType == 2) {
            self.cdjrSW.on = !self.cdjrSW.on;
            [ProgressHUD hideHUD];
            [ProgressHUD showSuccessMessage:NSLocalizedString(@"Set failed", nil)];
        }
    });
}
- (IBAction)actionPreHot:(id)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Heating start time (min)", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:nil]];
    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Confirm", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *timeIntervalTextfield = alertController.textFields.firstObject;
        NSString *timeInterval = timeIntervalTextfield.text;
        if (timeInterval.integerValue>0) {
            [self setPrehot:timeInterval.integerValue];
        }
    }]];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = NSLocalizedString(@"Enter preheating start time in minute", nil);
        textField.keyboardType = UIKeyboardTypeNumberPad;
    }];
    [self presentViewController:alertController animated:YES completion:nil];
}
- (void)setPrehot:(NSInteger)number {
    self.isWrite = YES;
    [ProgressHUD showActivityMessageInView:@"" timer:8];
    Byte write_array[11];
    write_array[0] = 0x01;
    write_array[1] = 0x10;
    write_array[2] = 0x25;
    write_array[3] = 0x00;
    write_array[4] = 0x00;
    write_array[5] = 0x01;
    write_array[6] = 0x02;
    write_array[7] = (number >> 8) & 0xFF;
    write_array[8] = number & 0xFF;
    write_array[9] = 0x00;
    write_array[10] = 0x00;
    sleep(1);
    [[BTManager sharedBTManager] sendCommandWithCrc:write_array length:11];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.isWrite = NO;
    });
}
- (IBAction)actionRestart:(UISwitch *)sender {
    self.isWrite = YES;
    BOOL isOpen = sender.on;
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"Confirm force start", nil) preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.isWrite = NO;
        self.restartSW.on = !self.restartSW.on;
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Confirm", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self setRestart:isOpen];
    }]];
    [self presentViewController:alertController animated:true completion:nil];
}

- (void)setRestart:(BOOL)isOpen {
    self.setType = 3;
    [ProgressHUD showActivityMessageInView:@"" timer:8];
    Byte write_array[8];
    write_array[0] = 0x01;
    write_array[1] = 0x06;
    write_array[2] = 0x11;
    write_array[3] = isOpen ? 0x02 : 0x03;
    write_array[4] = 0x00;
    write_array[5] = 0x0e;
    write_array[6] = 0x00;
    write_array[7] = 0x00;
    sleep(1);
    [[BTManager sharedBTManager] sendCommandWithCrc:write_array length:8];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.isWrite = NO;
        if (self.setType == 3) {
            self.cdjrSW.on = !self.cdjrSW.on;
            [ProgressHUD hideHUD];
            [ProgressHUD showSuccessMessage:NSLocalizedString(@"Set failed", nil)];
        }
    });
}
- (IBAction)actionUpgrade:(id)sender {
    UpgardeViewController *vc = [[UpgardeViewController alloc] initWithNibName:@"UpgardeViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - notification
- (void)didReciveData:(NSNotification *)notification {
    if (self.isUpgrading) return;
    NSData *reciveData = notification.object;
    Byte *dataBytes = (Byte*)reciveData.bytes;
    if (dataBytes[2]==0x18) { //3.4 主控读取电池其他信息
        NSInteger funcitonInfo = dataBytes[3+2*2+1]*256+dataBytes[3+2*2];
        NSInteger mosRelayMode = funcitonInfo&0x400;
        dispatch_async(dispatch_get_main_queue(), ^{
            self.cfdSW.on = mosRelayMode > 0;
        });
    }else if(dataBytes[2] == 0x02 && reciveData.length == 7) {
        NSInteger num = dataBytes[3] * 256 + dataBytes[4];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.cdjrSW.on = (num >> 12) & 1;
            self.fdjrSW.on = (num >> 13) & 1;
            self.restartSW.on = (num >> 14) & 1;
        });
    }else if (dataBytes[1] == 0x06) {
        self.setType = 0;
        [ProgressHUD hideHUD];
        [ProgressHUD showSuccessMessage:NSLocalizedString(@"Set successful", nil)];
        self.isWrite = NO;
    }else if (dataBytes[1] == 0x10) {
        self.setType = 0;
        [ProgressHUD hideHUD];
        [ProgressHUD showSuccessMessage:NSLocalizedString(@"Set successful", nil)];
        self.isWrite = NO;
    }else if (dataBytes[1] == 0x90) {
        self.setType = 0;
        [ProgressHUD hideHUD];
        [ProgressHUD showSuccessMessage:NSLocalizedString(@"Set failed", nil)];
        self.isWrite = NO;
    }
}

#pragma mark - sendData
- (void)readStateCHGDSG {
    //3.2 读取和设置输出状态信息命令
    Byte write_array[8];
    write_array[0] = 0x01;
    write_array[1] = 0x03;
    write_array[2] = 0xd1;
    write_array[3] = 0x15;
    write_array[4] = 0x00;
    write_array[5] = 0xc;
    write_array[6] = 0x6d;
    write_array[7] = 0x37;
    [[BTManager sharedBTManager] sendCommand:write_array length:8];
}
- (void)readSwitchStatus {
    Byte write_array[8];
    write_array[0] = 0x01;
    write_array[1] = 0x03;
    write_array[2] = 0xd1;
    write_array[3] = 0x17;
    write_array[4] = 0x00;
    write_array[5] = 0x01;
    write_array[6] = 0x00;
    write_array[7] = 0x00;
    [[BTManager sharedBTManager] sendCommandWithCrc:write_array length:8];
}

- (void)writeClearProtection {
    //6.4 6.1 清除保护记录
    Byte byte[] = {0x01,0x06,0x10,0x01,0x00,0x01,0x1d,0x0a};
    [[BTManager sharedBTManager] sendCommand:byte length:8];
    [self performSelector:@selector(writeResetProtection) withObject:nil afterDelay:1];
}

- (void)writeResetProtection {
    //6.2 保护参数复位
    Byte byte[] = {0x01,0x06,0x10,0x02,0x00,0x01,0xed,0x0a};
    [[BTManager sharedBTManager] sendCommand:byte length:8];
    [self performSelector:@selector(writeResetCombinParam) withObject:nil afterDelay:1];
}

- (void)writeResetCombinParam {
    //6.3 综合参数复位
    Byte byte[] = {0x01,0x06,0x10,0x03,0x00,0x01,0xbc,0xca};
    [[BTManager sharedBTManager] sendCommand:byte length:8];
    [self performSelector:@selector(writeResetHeatAndCooling) withObject:nil afterDelay:1];
}

- (void)writeResetHeatAndCooling {
    //6.4 加热&冷凝参数复位
    Byte byte[] = {0x01,0x06,0x10,0x04,0x00,0x01,0x0d,0x0b};
    [[BTManager sharedBTManager] sendCommand:byte length:8];
}

@end
